﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
namespace Presentation
{
    public partial class frmLoyaltyCard : Form
    {
        private LoyaltyCardBUL BO;
        private bool editMode;
        private string currIdentity;
        private string currEmail;
        private string currPhone;

        public frmLoyaltyCard()
        {
            InitializeComponent();
            BO = new LoyaltyCardBUL();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            formReset();
        }
        private void formReset()
        {
            this.txtCardId.Text = "";
            this.txtCusName.Text = "";
            this.txtEmail.Text = "";
            this.txtIdentityId.Text = "";
            this.txtPhoneNum.Text = "";
            this.chkbxSubcribe.Checked = false;
            errorProvider1.Clear();
            if (btnClear.Text.Equals("Cancel"))
            {
                dgvCards.ClearSelection();
                this.txtCusName.Enabled = true;
                this.txtIdentityId.Enabled = true;
                btnClear.Text = "Clear";
                btnAdd.Text = "Add";
                editMode = false;
            }
        }

        private void refresh()
        {
            DataTable dt = BO.LoadAll();
            this.dgvCards.DataSource = dt;
            this.dgvCards.ClearSelection();
        }
        private bool ValidateInput()
        {
            errorProvider1.Clear();
            bool valid = true;
            // validate input required
            if (txtCusName.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtCusName, "Please input customer name");
                valid = false;
            }
            if (txtPhoneNum.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtPhoneNum, "Please input customer phone");
                valid = false;
            }
            if (txtEmail.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtEmail, "Please input customer email");
                valid = false;
            }

            // phone is number only
            for (int i = 0; i < txtPhoneNum.Text.Length; i++)
            {

                if (!char.IsDigit(txtPhoneNum.Text, i))
                {
                    errorProvider1.SetError(txtPhoneNum, "Please input number only");
                    valid = false;
                    break;
                }
            }

            // identityNumber is number only
            for (int i = 0; i < txtIdentityId.Text.Length; i++)
            {

                if (!char.IsDigit(txtIdentityId.Text, i))
                {
                    valid = false;
                    errorProvider1.SetError(txtIdentityId, "Please input number only");
                    break;
                }
            }

            // validate existed in DB
            if (!editMode)
            {
                if (BO.CheckExisted("phoneNum", txtPhoneNum.Text.Trim()))
                {
                    errorProvider1.SetError(txtPhoneNum, "Already existed");
                    valid = false;
                }
                if (BO.CheckExisted("cusEmail", txtEmail.Text.Trim()))
                {
                    errorProvider1.SetError(txtEmail, "Already existed");
                    valid = false;
                }
                if (BO.CheckExisted("identityId", txtIdentityId.Text.Trim()))
                {
                    errorProvider1.SetError(txtIdentityId, "Already existed");
                    valid = false;
                }
            }
            else
            {
                if (!currPhone.Equals(txtPhoneNum.Text.Trim(), StringComparison.CurrentCultureIgnoreCase) && BO.CheckExisted("phoneNum", txtPhoneNum.Text.Trim()))
                {
                    errorProvider1.SetError(txtPhoneNum, "Already existed");
                    valid = false;
                }
                if (!currEmail.Equals(txtEmail.Text.Trim(), StringComparison.CurrentCultureIgnoreCase) && BO.CheckExisted("cusEmail", txtEmail.Text.Trim()))
                {
                    errorProvider1.SetError(txtEmail, "Already existed");
                    valid = false;
                }
                if (!currIdentity.Equals(txtIdentityId.Text.Trim(), StringComparison.CurrentCultureIgnoreCase) && BO.CheckExisted("identityId", txtIdentityId.Text.Trim()))
                {
                    errorProvider1.SetError(txtIdentityId, "Already existed");
                    valid = false;
                }
            }

            return valid;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ValidateInput())
            {
                return;
            }
            int cardId = int.Parse("0" +this.txtCardId.Text);
            string cusname = this.txtCusName.Text;
            string email = this.txtEmail.Text;
            string identityId = this.txtIdentityId.Text;
            string phoneNum = this.txtPhoneNum.Text;
            bool subcribe = this.chkbxSubcribe.Checked;
            bool result = false;

            if (btnAdd.Text.Equals("Add"))
            {
                result = BO.AddNewCard(cusname, email, identityId, phoneNum, subcribe);

            }
            else
            {
                result = BO.UpdateCard(cardId, email, phoneNum, subcribe);
            }
            if (result)
            {
                MessageBox.Show("Successful");
                refresh();
                formReset();
            }
            else
            {
                MessageBox.Show("Fail");
                formReset();
            }
        }

        private void frmLoyaltyCard_Load(object sender, EventArgs e)
        {
            DataTable dt = BO.LoadAll();
            this.dgvCards.DataSource = dt;

            formReset();

        }

        private void dgvCards_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCards.SelectedRows.Count == 0)
                return;
            if (dgvCards.SelectedRows[0].Cells[0].Value == null)
                return;

            // data binding
            btnAdd.Text = "Update";
            btnClear.Text = "Cancel";

            this.txtCardId.Text = dgvCards.SelectedRows[0].Cells[0].Value.ToString();
            this.txtCusName.Text = dgvCards.SelectedRows[0].Cells[1].Value.ToString();
            this.txtPhoneNum.Text = dgvCards.SelectedRows[0].Cells[2].Value.ToString();
            this.txtIdentityId.Text = dgvCards.SelectedRows[0].Cells[3].Value.ToString();
            this.txtEmail.Text = dgvCards.SelectedRows[0].Cells[6].Value.ToString();
            bool subcribe = bool.Parse(dgvCards.SelectedRows[0].Cells[7].Value.ToString());
            this.chkbxSubcribe.Checked = subcribe;

            this.txtCusName.Enabled = false;
            this.txtIdentityId.Enabled = false;
            this.currEmail = txtEmail.Text;
            this.currPhone = txtPhoneNum.Text;
            this.currIdentity = txtIdentityId.Text;
            this.editMode = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchKey = this.txtSearch.Text;
            if (searchKey.Trim().Length == 0)
            {
                DataTable dt = BO.LoadAll();
                this.dgvCards.DataSource = dt;
            }
            else
            {
                DataTable dt = BO.LoadSearch(searchKey);
                this.dgvCards.DataSource = dt;
            }
            this.dgvCards.ClearSelection();
            formReset();
        }

        private void frmLoyaltyCard_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmMain main = new frmMain();
                this.Hide();
                main.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
